package com.wedo.mall.activity.user;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;

import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.utils.ESPUtils;
import com.wedo.mall.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindViews;
import butterknife.OnClick;
import io.reactivex.Observable;

public class LanguageActivity extends EActivity {

    public static final int LANGUAGE_CHINESE = 0;
    public static final int LANGUAGE_ENGLISH = 1;
    private int language = LANGUAGE_ENGLISH;

    @BindViews({R.id.iv_Chinese,R.id.iv_English})
    List<View> ivs;

    @Override
    protected int layout() {
        return R.layout.activity_language;
    }

    @Override
    protected void onActivityCreated(Bundle state) {

        String LString = ESPUtils.getString(this, LANGUAGE, ENGLISH);
        if (LString.equals(CHINESE)) {
            language = LANGUAGE_CHINESE;
        } else if (LString.equals(ENGLISH)) {
            language = LANGUAGE_ENGLISH;
        }
        onLanguageChanged();
    }

    private void onLanguageChanged(){
        for (View v : ivs) {
            v.setVisibility(View.INVISIBLE);
        }
        ivs.get(language).setVisibility(View.VISIBLE);
    }


    private void alert(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.dialog_tip);
        builder.setMessage(R.string.dialog_switch_language);
        builder.setNegativeButton(R.string.cancel,null).setPositiveButton(R.string.dialog_restart, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                restart();
            }
        });

        builder.show();
    }

    private void restart(){
        Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    private void postLanguage(){
        this.loading();
        Map<String,String> params = new HashMap<>(2);
        if(language == LANGUAGE_CHINESE){
            params.put("language","zh-cn");
        }else if(language == LANGUAGE_ENGLISH){
            params.put("language","en-us");
        }
        params.put("jump","1");
        Observable<EResponse> observable = EClient.with().api().post(EApi.USER_CHOOSELANGUAGE,params);
        EClient.with().observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                LanguageActivity.this.dismiss();
                if(language == LANGUAGE_CHINESE){
                    cn();
                    alert();
                }else if(language == LANGUAGE_ENGLISH){
                    en();
                    alert();
                }

            }

            @Override
            public void failure(String msg) {
                LanguageActivity.this.dismiss(msg);
            }
        });
    }

    @OnClick({R.id.rl_Chinese,R.id.rl_English})
    protected void onClick(View view){
        switch (view.getId()){
            case R.id.rl_Chinese:
                language = LANGUAGE_CHINESE;
                postLanguage();
                break;
            case R.id.rl_English:
                language = LANGUAGE_ENGLISH;
                postLanguage();
                break;
        }
        onLanguageChanged();
    }

}
